package com.gjy.leetcode.interview;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-19 16:42:11
 */
public class B0105 {

    @Test
    public void test1() {
        String first = "pale", second = "ple";
        boolean b = oneEditAway(first, second);
        Assertions.assertTrue(b);
    }

    @Test
    public void test2() {
        String first = "pales", second = "pal";
        boolean b = oneEditAway(first, second);
        Assertions.assertFalse(b);
    }

    public boolean oneEditAway(String first, String second) {
        int p1 = 0;
        int q1 = first.length() - 1;
        int p2 = 0;
        int q2 = second.length() - 1;
        while (p1 <= q1 && p2 <= q2) {
            if (first.charAt(p1) == second.charAt(p1)) {
                ++p1;
                ++p2;
                continue;
            }
            if (first.charAt(q1) == second.charAt(q2)) {
                --q1;
                --q2;
            } else {
                break;
            }
        }
        return q1 - p1 + q2 - p2 <= 0 && Math.abs(p1 - p2) <= 1 && Math.abs(q1 - q2) <= 1;
    }
}
